/*__________________________________________________________*/
/*__________________________________________________________*/
/**
 * Fichier : DAOSQLCategorieSport.java
 * 
 * créé le : 28 févr. 2010 à 10:04:08
 *
 * Auteur : Stéphane DOLADILLE
 */
/*__________________________________________________________*/
package persistence.DAO.SQL.entities;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;


import metier.Bean;
import metier.CategorieSport;
import persistence.ConnectionManager;
import persistence.DAO.DAO;
import persistence.DAO.DAOSQL;

/*__________________________________________________________*/
/**
 */
/*__________________________________________________________*/
public class DAOSQLCategorieSport extends DAOSQL
{

	private static DAO instance;
	private static final Logger LOGGER = Logger.getLogger(DAOSQLArticle.class);
		
	public static DAO getInstance() {
		if (instance == null)
			instance = new DAOSQLCategorieSport();
		return instance;
	}
	
	private DAOSQLCategorieSport() {
		try {
			insert = prepareInsert();
			update = prepareUpdate();
			delete = prepareDelete();
			select = prepareSelect();
		    selectAll = prepareSelectAll();		
		} catch (Exception e) {
			LOGGER.error("Erreur lors de la création des PreparedStatement");
		}
	}
	
	
	
	/*__________________________________________________________*/
	/**
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @see persistence.DAO.DAOSQL#prepareDelete()
	 */
	@Override
	protected PreparedStatement prepareDelete() throws SQLException,
			ClassNotFoundException
	{
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("DELETE FROM categoriesport WHERE idCategorieSport=?");
		return ps;
	}

	/*__________________________________________________________*/
	/**
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @see persistence.DAO.DAOSQL#prepareInsert()
	 */
	@Override
	protected PreparedStatement prepareInsert() throws SQLException,
			ClassNotFoundException
	{
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("INSERT INTO categoriesport " +
				"(nom) VALUES (?)");
		return ps;
	}

	/*__________________________________________________________*/
	/**
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @see persistence.DAO.DAOSQL#prepareSelect()
	 */
	@Override
	protected PreparedStatement prepareSelect() throws SQLException,
			ClassNotFoundException
	{
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("SELECT * FROM categoriesport WHERE idCategorieSport=?");
		return ps;
	}

	/*__________________________________________________________*/
	/**
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @see persistence.DAO.DAOSQL#prepareSelectAll()
	 */
	@Override
	protected PreparedStatement prepareSelectAll() throws SQLException,
			ClassNotFoundException
			{
				PreparedStatement ps = null;
				ps = ConnectionManager.getInstance().getConnection().prepareStatement("SELECT * FROM categoriesport");
				return ps;
			}

	/*__________________________________________________________*/
	/**
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 * @see persistence.DAO.DAOSQL#prepareUpdate()
	 */
	@Override
	protected PreparedStatement prepareUpdate() throws SQLException,
			ClassNotFoundException
	{
		PreparedStatement ps = null;
		ps = ConnectionManager.getInstance().getConnection().prepareStatement("UPDATE categoriesport SET " +
				"nom=? WHERE idCategorieSport=?");
		return ps;
	}

	/*__________________________________________________________*/
	/**
	 * @param bean
	 * @return
	 * @throws Exception
	 * @see persistence.DAO.DAO#effacer(metier.Bean)
	 */
	@Override
	public boolean effacer(Bean bean) throws Exception
	{
		boolean retour = false;
		try {
			ConnectionManager.getInstance().getConnection();
			delete.clearParameters();
			delete.setInt(1, bean.getId());
			delete.executeUpdate();
			retour = true;
		} catch (SQLException e) {
			LOGGER.error("Erreur lors de la suppression d'une categorie forum");
		} catch (ClassNotFoundException e) {
			LOGGER.error("Impossible d'obtenir la connexion");
		}		
		return retour;
	}

	/*__________________________________________________________*/
	/**
	 * @param bean
	 * @return
	 * @throws Exception
	 * @see persistence.DAO.DAO#enregistrer(metier.Bean)
	 */
	@Override
	public boolean enregistrer(Bean bean) throws Exception
	{
		boolean retour = false;
		if (0 == bean.getId()) {
			retour = create(bean);
		} else {
			retour = update(bean);
		}
		return retour;
	}
	
	private boolean create(Bean bean)
	{
		boolean retour = false;
		CategorieSport cat = (CategorieSport) bean;
		try {
			ConnectionManager.getInstance().getConnection();
			insert.clearParameters();
			insert.setString(1, cat.getNom());
			insert.executeUpdate();
			retour = true;
		} catch (SQLException e) {
			LOGGER.error("Erreur lors de la création d'une catégorie de sport");
		} catch (ClassNotFoundException e) {
			LOGGER.error("Impossible d'obtenir la connexion");
		}		
		return retour;
	}
	
	private boolean update(Bean bean) {
		boolean retour = false;
		CategorieSport cat = (CategorieSport) bean;
		try {
			ConnectionManager.getInstance().getConnection();
			update.clearParameters();
			update.setString(1, cat.getNom());
			update.setInt(7, cat.getId());
			update.executeUpdate();
			retour = true;
		} catch (SQLException e) {
			LOGGER.error("Erreur lors de la modification d'une catégorie de sport");
		} catch (ClassNotFoundException e) {
			LOGGER.error("Impossible d'obtenir la connexion");
		}		
		return retour;
	}

	/*__________________________________________________________*/
	/**
	 * @return
	 * @see persistence.DAO.DAO#getBeans()
	 */
	@Override
	public List<Bean> getBeans()
	{
	ResultSet rs = null;
	List<Bean> beans = new ArrayList<Bean>();
	try {
		ConnectionManager.getInstance().getConnection();
		rs = selectAll.executeQuery();
		while (rs.next()) {
			CategorieSport cat = new CategorieSport();
			cat.setId(Integer.parseInt(rs.getString("idCategorieSport")));
			cat.setNom(rs.getString("nom"));
			beans.add(cat);
		}
	} catch (SQLException e) {
		LOGGER.error("Erreur lors de la préparation du PreparedStatement");
	} catch (ClassNotFoundException e) {
		LOGGER.error("impossible d'obtenir la connexion");
	}
	return beans;
}

	/*__________________________________________________________*/
	/**
	 * @param id
	 * @return
	 * @see persistence.DAO.DAO#selectionner(int)
	 */
	@Override
	public Bean selectionner(int id)
	{
		Bean bean = null;
		try {
			ConnectionManager.getInstance().getConnection();
			select.clearParameters();
			select.setInt(1, id);
			ResultSet rs = select.executeQuery();
			while (rs.next()) {
				CategorieSport cat = new CategorieSport();
				cat.setId(Integer.parseInt(rs.getString("idCategorieSport")));
				cat.setNom(rs.getString("nom"));
				bean = cat;
			}
		} catch (SQLException e) {
			LOGGER.error("Erreur lors de la préparation du PreparedStatement");
		} catch (ClassNotFoundException e) {
			LOGGER.error("impossible d'obtenir la connexion");
		}
		return bean;
	}


}

/*__________________________________________________________*/
/*   Fin du fichier DAOSQLCategorieSport.java
/*__________________________________________________________*/